// pages/comment/comment.js
import {network} from "../index/network"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total:0,
    start:1,
    count:10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var rate = parseInt(options.rate);
    options.rate = rate && rate > 0 ? rate.toFixed(1) :　"未评分";
    that.setData(options);  
    that.getComments(1);  
  },

  getComments:function(start,count=this.data.count){
    var that = this;
    var id = that.data.id;
    if (start > that.data.start) {
      that.setData({
        nextLoading:true
      });
    }else{
      that.setData({
        preLoading:true
      })
    }
    network.getComment({
      id:id,
      start:start,
      count:count,
      success:function(data){
        var total = data.total;
        var comments = data.interests;
        console.log(comments);
        that.setData({
          total:total,
          comments:comments,
          start:start,
          nextLoading:false,
          preLoading:false
        })
      }
    })
  },

  onItemTapEvent:function(event){
    wx.navigateBack();
  },

  onPrePageTap:function(event){
    var that = this;
    var oldStart = that.data.start;
    if (oldStart - that.data.count > 0) {
      var start = oldStart - that.data.count;
      that.getComments(start);
    }
  },

  onNextPageTap:function(event){
    var that = this;
    var oldStart = that.data.start;
    var start = oldStart + that.data.count;
    that.getComments(start);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})